home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / nd92.lha / ASync / ASync.txt < prev    next >
Encoding:
Text File  |  1992-12-21  |  4.6 KB  |  97 lines

  1. (c)  Copyright 1992 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice,
  3. and is provided "as is" without warranty of any kind, either expressed
  4. or implied.  The entire risk as to the use of this information is
  5. assumed by the user.
  6.  
  7.  
  8. Fast AmigaDOS I/O
  9.  
  10.  
  11. by Martin Taillefer
  12.  
  13.  
  14. Reading and writing data is crucial to most applications and is, in
  15. many cases, a major bottleneck.  Using the Amiga's sophisticated file
  16. system architecture can help reduce, and sometimes eliminate, the
  17. time spent waiting for I/O to complete.  This article presents six
  18. small routines that can greatly improve an application's I/O
  19. performance.
  20.  
  21. Typically, an application processes a file in the following manner:
  22.  
  23. Step 1: Open the file.
  24. Step 2: Read some data (with the DOS library's Read() function).
  25. Step 3: Process that data.
  26. Step 4: Repeat steps 2 and 3 until the application is finished
  27.          processing the file.
  28. Step 5: Close file.
  29.  
  30. This sequence of steps is effective, but it does have a potential
  31. bottleneck.  Whenever the application reads some data using the DOS
  32. Read() function, the Amiga has to put that task to sleep and ask the
  33. file system to fetch the data.  The file system then starts up the
  34. disk hardware and reads the data.  After the file system finishes
  35. reading the data, the operating system wakes up the application.
  36.  
  37. The problem is step 2.  While the file system is busy reading data
  38. from the disk, the application is idle, waiting for the DOS I/O in
  39. Read() to complete.  A more sophisticated application would initiate
  40. an asynchronous read, allowing the application to continue to do some
  41. other important chore while the file system is busy reading.  If all
  42. goes well, the file system will be finished with the asynchronous
  43. read by the time the application is finished with its chore, so the
  44. application will not have to wait for any DOS I/O to complete before
  45. the application can access data.
  46.  
  47. Using the routines presented in this article, an application
  48. processes a file in the following manner:
  49.  
  50. Step 1:  Open the file with OpenAsync().  This function opens the
  51.           file and, if the file is opened for reading, OpenAsync()
  52.           asks the file system to start reading data, asynchronously.
  53. Step 2:  Read some data with ReadAsync().  If the asynchronous read
  54.           request that OpenAsync() sent has not completed, ReadAsync()
  55.           will put the application to sleep until that request
  56.           returns. Ideally, the read will have returned, so the
  57.           application won't have to wait. ReadAsync() will also
  58.           initiate a new asynchronous read so new file data is ready
  59.           when the application needs it.
  60. Step 3:  Process the file data.
  61. Step 4:  Repeat steps 2 and 3 until the application processes
  62.           all its file data.
  63. Step 5:  Close the file with CloseAsync().
  64.  
  65. Immediately after opening the file, OpenAsync() sends a request to
  66. the file system to get it reading data in the background.  If all
  67. goes well, by the time the application gets around to reading the
  68. first byte of data, the file system has already copied the data into
  69. memory.  That means the application doesn't need to wait and can
  70. immediately start processing the data.  As soon as the application
  71. starts processing data from the file using ReadAsync(), ReadAsync()
  72. sends out a second request to the file system to fill up a second
  73. buffer.  Once the application is done processing the first buffer, it
  74. starts processing the second one.  When this happens, the file system
  75. starts filling up the first buffer again with new data.  This process
  76. continues until the application has read all of its data.  This
  77. technique is known as ``double-buffered asynchronous I/O''.
  78.  
  79. The set of functions presented below offer high-performance I/O using
  80. the technique described above.  The interface is very similar to
  81. standard AmigaDOS files.  These routines enable full asynchronous
  82. read/write of any file.
  83.  
  84. These functions are especially useful on an Amiga with a DMA (Direct
  85. Memory Access) hard drive.  DMA makes it possible to transfer data to
  86. memory at the same time the CPU is busy executing a task's
  87. instructions.  A DMA data transfer is truly parallel, so, under
  88. normal conditions, the CPU is operating at full speed, unaffected by
  89. the DMA transfer.  This parallelism is what makes the set of
  90. accompanying routines so efficient.  They exploit the fact that the
  91. Amiga can transfer an application's data while the application is
  92. busy processing other data.
  93.  
  94. Although these asynchronous routines make disk I/O much faster, they
  95. do have an important limitation.  The routines do not support seeking
  96. into a file.
  97.